import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class Main:
    # 链接超时秒数
    TIMEOUT_SECOND = 5
    # 切换frame等待秒数
    FRAME_SWITCH_DELAY = 1

    # base url
    WEBSITE_URL = 'http://localhost:9913'
    CONTEXT = '/hynyct'
    BASE_URL = WEBSITE_URL + CONTEXT

    # 登录账号密码
    LOGIN_USERNAME = 'xt_admin'
    LOGIN_PASSWORD = '111111'

    # 支持页面类型：简单列表、上下表格、左树右列表、左树右上下表格、上表下图、审批、自定义
    page_type = ('single_list', 'top_list_bottom_list', 'left_tree_right_list', 'left_tree_top_list_bottom_list', 'top_list_bottom_chart', 'appoval_list', 'custom')

    # 公用的模拟浏览器对象
    browser = None

    def run(self):
        self.init()

        # 登录
        self.login()

        # 获取导航栏
        top_nav_list = self.nav()

        # 遍历所有顶级导航
        for nav_index, nav in enumerate(top_nav_list):
            print(nav.text)
            if nav_index != 1:
                continue

            # 点击顶级导航
            if nav_index != 0:
                nav.click()

            # todo 需要动态获取是否切换成功
            time.sleep(self.FRAME_SWITCH_DELAY)

            # 获取功能菜单，并构造需要遍历的功能列表
            menu_tree = self.browser.find_element_by_id('menuTree').find_elements_by_css_selector('[outlinelevel="2"]')
            for menu_index, menu in enumerate(menu_tree):
                menu_name = menu.text
                url = menu.get_attribute('url')
                print('【INFO】当前访问功能：' + menu_name + ',链接为：' + self.BASE_URL + url)

                # todo 临时测试使用
                if menu_index != 0:
                    break

                # 点击进入功能菜单
                menu.click()
                # todo 需要动态获取是否切换成功
                time.sleep(self.FRAME_SWITCH_DELAY)
                main_frame = self.browser.find_element_by_css_selector('[src="' + self.CONTEXT + url + '"]')
                self.browser.switch_to.frame(main_frame)

                # 判断页面类型，并根据不同类型采用不同适配器测试
                page_type = self.recognize_page_type()
                self.test_feature_page(page_type)

    def init(self):
        options = webdriver.ChromeOptions()
        self.browser = webdriver.Chrome(chrome_options=options)
        self.browser.maximize_window()

    def login(self):
        print('【INFO】登录...')
        document = self.browser.get(self.BASE_URL + '/login')
        self.browser.find_element_by_id('loginName').send_keys(self.LOGIN_USERNAME)
        self.browser.find_element_by_id('password').send_keys(self.LOGIN_PASSWORD)
        self.browser.find_element_by_id('loginBtn').click()
        print(document)

    def nav(self):
        print('【INFO】访问顶级导航栏...')
        return self.browser.find_element_by_id('topmenu').find_element_by_class_name('l-topmenu-left').find_elements_by_tag_name('div')[1].find_elements_by_tag_name('a')

    def recognize_page_type(self):
        page_source = self.browser.page_source
        # todo 判断什么类型页面

        # 单列表

        # 上下表格

        # 左树右列表

        # 左树右上下表格

        # 上表下图

        # 自定义

        return 'simple_list'

    def test_feature_page(self, page_type):
        if page_type == 'simple_list':
            self.simple_list_handler()
        elif page_type == '':
            print('【WARNING】' + page_type + '不能自动测试')
            pass
        else:
            print('【WARNING】' + page_type + '不能自动测试')
            pass

    def simple_list_handler(self):

        # 遍历按钮（新增、编辑、查看、删除、关闭、提交、重新编辑、签收、办理、其它）
        toolbar_buttons = self.browser.find_element_by_css_selector('[class="l-panel-topbarinner l-toolbar l-panel-topbarinner-left"]').find_elements_by_css_selector('[class="l-toolbar-item l-panel-btn l-toolbar-item-hasicon"]')

        for button_index, button in enumerate(toolbar_buttons):
            if button_index != 0:
                break

            button.click()
            # todo
            time.sleep(self.FRAME_SWITCH_DELAY)

            # todo 完善simple_list

            # 弹窗

            # 录入（文本框、文本域、金额、单选、多选、下拉字典（单选）、下拉字典（多选）、下拉树、下拉表格、其它）

            # 提交/关闭
        pass


if __name__ == '__main__':
    main = Main()
    main.run()
